home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / tutor / cptuts22.arj / INHERIT5.CPP < prev    next >
C/C++ Source or Header  |  1992-01-20  |  3KB  |  124 lines

  1.                                   // Chapter 8 - Program 5
  2. #include <iostream.h>
  3.  
  4. class vehicle {
  5.    int wheels;
  6.    float weight;
  7. public:
  8.    void initialize(int in_wheels, float in_weight);
  9.    int get_wheels(void) {return wheels;}
  10.    float get_weight(void) {return weight;}
  11.    float wheel_loading(void) {return weight/wheels;}
  12. };
  13.  
  14.  
  15.  
  16.  
  17. class car : public vehicle {
  18. private:
  19.    int passenger_load;
  20. public:
  21.    void initialize(int in_wheels, float in_weight, int people = 4);
  22.    int passengers(void) {return passenger_load;}
  23. };
  24.  
  25.  
  26.  
  27.  
  28. class truck : public vehicle {
  29. private:
  30.    int passenger_load;
  31.    float payload;
  32. public:
  33.    void init_truck(int how_many = 2, float max_load = 24000.0);
  34.    float efficiency(void);
  35.    int passengers(void) {return passenger_load;}
  36. };
  37.  
  38.  
  39.  
  40.  
  41. main()
  42. {
  43. vehicle unicycle;
  44.  
  45.    unicycle.initialize(1, 12.5);
  46.    cout << "The unicycle has " <<
  47.                                 unicycle.get_wheels() << " wheel.\n";
  48.    cout << "The unicycle's wheel loading is " <<
  49.          unicycle.wheel_loading() << " pounds on the single tire.\n";
  50.    cout << "The unicycle weighs " <<
  51.                              unicycle.get_weight() << " pounds.\n\n";
  52.  
  53. car sedan;
  54.  
  55.    sedan.initialize(4, 3500.0, 5);
  56.    cout << "The sedan carries " << sedan.passengers() <<
  57.                                                     " passengers.\n";
  58.    cout << "The sedan weighs " << sedan.get_weight() << " pounds.\n";
  59.    cout << "The sedan's wheel loading is " <<
  60.                     sedan.wheel_loading() << " pounds per tire.\n\n";
  61.  
  62. truck semi;
  63.  
  64.    semi.initialize(18, 12500.0);
  65.    semi.init_truck(1, 33675.0);
  66.    cout << "The semi weighs " << semi.get_weight() << " pounds.\n";
  67.    cout << "The semi's efficiency is " <<
  68.                           100.0 * semi.efficiency() << " percent.\n";
  69. }
  70.  
  71.  
  72.  
  73.  
  74.               // initialize to any data desired
  75. void
  76. vehicle::initialize(int in_wheels, float in_weight)
  77. {
  78.    wheels = in_wheels;
  79.    weight = in_weight;
  80. }
  81.  
  82.  
  83.  
  84. void
  85. car::initialize(int in_wheels, float in_weight, int people)
  86. {
  87.    passenger_load = people;
  88.    vehicle::initialize(in_wheels, in_weight);
  89. }
  90.  
  91.  
  92.  
  93.  
  94. void
  95. truck::init_truck(int how_many, float max_load)
  96. {
  97.    passenger_load = how_many;
  98.    payload = max_load;
  99. }
  100.  
  101.  
  102.  
  103. float
  104. truck::efficiency(void)
  105. {
  106.    return payload / (payload + get_weight());
  107. }
  108.  
  109.  
  110.  
  111.  
  112. // Result of execution
  113. //
  114. // The unicycle has 1 wheel.
  115. // The unicycle's wheel loading is 12.5 pounds on the single tire.
  116. // The unicycle weighs 12.5 pounds.
  117. //
  118. // The sedan carries 5 passengers.
  119. // The sedan weighs 3500 pounds.
  120. // The sedan's wheel loading is 875 pounds per tire.
  121. //
  122. // The semi weighs 12500 pounds.
  123. // The semi's efficiency is 72.929072 percent.
  124.